package vip.websky.project.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.error.CommonsRuntimeException;
import vip.websky.project.common.error.ErrorCode;
import vip.websky.project.common.error.ErrorMessage;
import vip.websky.project.common.utils.DateUtils;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysPermissionBO;
import vip.websky.project.module.dto.RequestDTO;
import vip.websky.project.module.dto.ResponseDTO;
import vip.websky.project.module.dto.request.user.AddPermissionReqDTO;
import vip.websky.project.module.dto.request.user.EditPermissionReqDTO;
import vip.websky.project.module.dto.request.user.GetPermissionReqDTO;
import vip.websky.project.module.dto.response.user.SysPermissionRespDTO;
import vip.websky.project.user.error.UserErrorCode;
import vip.websky.project.user.error.UserErrorMessage;
import vip.websky.project.user.server.PermissionServer;
import vip.websky.project.user.service.PermissionService;

import java.util.List;

/**
 * 系统权限 接口实现类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/25-2:08
 **/
@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionServer permissionServer;

    @KingLog
    @Override
    public ResponseDTO addPermission(RequestDTO requestDTO) {
        ResponseDTO<SysPermissionRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            AddPermissionReqDTO addSysPermission = JSON.parseObject(requestDTO.getParam(), AddPermissionReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysPermissionBO permissionBO = ObjectConvertUtils.copyToDest(addSysPermission, SysPermissionBO.class);
            //2.处理
            permissionBO.setCreateTime(DateUtils.getTimestamp());
            permissionBO = permissionServer.insertPermission(permissionBO);
            //3.返回结果
            if (permissionBO != null) {
                SysPermissionRespDTO respDTO = new SysPermissionRespDTO();
                ObjectConvertUtils.convert(permissionBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.PERMISSION_ADD_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.PERMISSION_ADD_FAIL);
                log.error("添加模块标识失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO getPermission(RequestDTO requestDTO) {
        ResponseDTO<SysPermissionRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetPermissionReqDTO getPermissionDTO = JSON.parseObject(requestDTO.getParam(), GetPermissionReqDTO.class);
            //1.参数转换
            SysPermissionBO permissionBO = ObjectConvertUtils.copyToDest(getPermissionDTO, SysPermissionBO.class);
            //2.处理
            permissionBO = permissionServer.selectPermission(permissionBO);
            //3.返回结果
            if (permissionBO != null) {
                SysPermissionRespDTO respDTO = new SysPermissionRespDTO();
                ObjectConvertUtils.convert(permissionBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.PERMISSION_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.PERMISSION_NOT_EXIST);
                log.error("获取信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO getPermissionList(RequestDTO requestDTO) {
        ResponseDTO responseDTO = new ResponseDTO<>();
        try {
            //请求参数
            GetPermissionReqDTO getPermissionDTO = JSON.parseObject(requestDTO.getParam(), GetPermissionReqDTO.class);
            //TODO 参数校验
            //参数转换
            SysPermissionBO sysPermissionBO = ObjectConvertUtils.copyToDest(getPermissionDTO, SysPermissionBO.class);
            //处理
            List<SysPermissionBO> permissionBOList = permissionServer.selectPermissionList(sysPermissionBO);
            //返回
            if (permissionBOList.size() > 0) {
                responseDTO.setResult(permissionBOList);//数据
            } else {
                responseDTO.setStatusCode(UserErrorCode.PERMISSION_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.PERMISSION_NOT_EXIST);
                log.error("获取列表失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO editPermission(RequestDTO requestDTO) {
        ResponseDTO<SysPermissionRespDTO> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            EditPermissionReqDTO editPermissionReqDTO = JSON.parseObject(requestDTO.getParam(), EditPermissionReqDTO.class);
            // TODO: 校验参数
            //1.参数转换
            SysPermissionBO permissionBO = ObjectConvertUtils.copyToDest(editPermissionReqDTO, SysPermissionBO.class);
            //2.处理
            permissionBO.setUpdateTime(DateUtils.getTimestamp());
            permissionBO = permissionServer.updatePermission(permissionBO);
            //3.返回结果
            if (permissionBO != null) {
                SysPermissionRespDTO respDTO = new SysPermissionRespDTO();
                ObjectConvertUtils.convert(permissionBO, respDTO);
                responseDTO.setResult(respDTO);
            } else {
                responseDTO.setStatusCode(UserErrorCode.PERMISSION_EDIT_FAIL);
                responseDTO.setStatusMessage(UserErrorMessage.PERMISSION_EDIT_FAIL);
                log.error("修改信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }

    @KingLog
    @Override
    public ResponseDTO removePermission(RequestDTO requestDTO) {
        ResponseDTO<Boolean> responseDTO = new ResponseDTO<>();
        try {
            //0.请求参数
            GetPermissionReqDTO getPermissionDTO = JSON.parseObject(requestDTO.getParam(), GetPermissionReqDTO.class);
            //参数转换
            SysPermissionBO permissionBO = ObjectConvertUtils.copyToDest(getPermissionDTO, SysPermissionBO.class);
            //2.处理
            boolean result = permissionServer.delPermission(permissionBO);
            //3.返回结果
            if (result) {
                responseDTO.setResult(result);
            } else {
                responseDTO.setStatusCode(UserErrorCode.PERMISSION_NOT_EXIST);
                responseDTO.setStatusMessage(UserErrorMessage.PERMISSION_NOT_EXIST);
                log.error("删除信息失败");
            }
        } catch (CommonsRuntimeException e) {
            responseDTO.setStatusCode(e.getCode());
            responseDTO.setStatusMessage(e.getMessage());
            log.error("自定义异常:{},{}", e.getCode(), e.getMessage());
        } catch (Exception e) {
            responseDTO.setStatusCode(ErrorCode.UNEXPECTED_ERROR);
            responseDTO.setStatusMessage(ErrorMessage.UNEXPECTED_ERROR);
            log.error("系统异常:{}", e);
        }
        return responseDTO;
    }
}
